package com.elm.deliveryaddress.controller;

import com.elm.common.pojo.Result;
import com.elm.deliveryaddress.pojo.DeliveryAddress;
import com.elm.deliveryaddress.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/deliveryAddress")
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @GetMapping("/listByUserId")
    public Result<List<DeliveryAddress>> listDeliveryAddressByUserId(String userId){
        List<DeliveryAddress> list=deliveryAddressService.listDeliveryAddressByUserId(userId);
        return Result.success(list);
    }

    @GetMapping("/getById")
    public Result<DeliveryAddress> getDeliveryAddressById(@RequestParam Integer daId){
        DeliveryAddress deliveryAddress=deliveryAddressService.getDeliveryAddressById(daId);
        if(deliveryAddress!=null){
            return Result.success(deliveryAddress);
        }

        return Result.error("查询失败");

    }

    @PostMapping("/save")
    public Result<Integer> saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        Integer rows=deliveryAddressService.saveDeliveryAddress(deliveryAddress);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("添加失败");
    }

    @PutMapping("/update")
    public Result<Integer> updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        Integer rows=deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("添加失败");
    }

    @DeleteMapping("/remove")
    public Result<Integer> removeDeliveryAddress(@RequestParam Integer daId){
        Integer rows=deliveryAddressService.removeDeliveryAddress(daId);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("删除失败");
    }



}
